<?php

namespace App\Repositories\Admin;

use App\BaseRepository;
use Illuminate\Support\Facades\DB;
use function PHPSTORM_META\map;

class UserRelationRepository extends BaseRepository
{
    //模型名称
    protected $modelName = "App\Model\UserRelation";

    protected $allowApiMethod = ['index','show'];

    //获取结果集
    protected function getListItems()
    {

        $this->processFilter();//过滤

        $this->processSearch();//搜索

        $this->processSort();//过滤

        $this->processPages();//分页

        $this->processQueryModel = $this->beforeListShow($this->processQueryModel);

        $items = $this->processQueryModel->get();

        $items = $items->map(function ($item){
            $data = ['id'=>$item->user->id,'name'=>$item->user->name,'avatar'=>$item->user->avatar,
                'created_at'=>$item->user->created_at->toDateTimeString()];
            $data['createBonus'] = $item->user->createBonuses()->where('bonus_user_id','=',$item->parent_id)->sum('amount');
            $item->userInfo = $data;
            $item->setHidden(['user']);
            return $item;
        });

        return $items;
    }
}